// 回车注册事件：新增前台防刷
$(document).ready(function () {
    $(document).keyup(function (e) {//捕获文档对象的按键弹起事件
        if (e.keyCode == 13) {//按键信息对象以参数的形式传递进来了
            //此处编写用户敲回车后的代码
            var res = $('#register_').attr('disabled');
            // console.log(res);
            if (typeof res === "undefined") {
                $("#register_").click();
            }
        }
    });
});

$(document).ready(function () {
// 在注册页面的时候
    $("#login_").click(function () {
        $('#register-modal').modal('hide');
        $('#login-modal').modal('show');

    });

    $("#register_").click(function () {
        var $email_ = $("#email_");
        var $confirmPassword_ = $("#confirmPassword_");
        var $password_ = $("#password_");

        var $emailVal_ = $email_.val();
        var $confirmPasswordVal_ = $confirmPassword_.val();
        var $passwordVal_ = $password_.val();

        if ($emailVal_ == "") {
            toastr.warning('邮箱不能为空');
            $email_.focus();
            return;
        }

        if ($passwordVal_ == "") {
            toastr.warning('密码不能为空');
            $password_.focus();
            return;
        }

        if ($confirmPasswordVal_ == "") {
            toastr.warning('再次输入密码不能为空');
            $confirmPassword_.focus();
            return;
        }

        if (checkEmail($emailVal_) == false) {
            // debugger;

            toastr.warning('请输入合法的邮箱');
            $email_.focus();
            return;
        }

        if ($confirmPasswordVal_ != $passwordVal_) {
            toastr.warning('两次输入密码不一致');
            $password_.focus();
            return;
        }

        // 禁用
        $('#register_').attr('disabled', "true");
        toastr.info('请稍做等待');
        $.ajax({
            url: "/api/register",
            traditional: true, // 使用传统解析，重要！
            type: "POST",
            data: {"email": $emailVal_, "password": $passwordVal_},
            dataType: "json",
            success: function (res) {
                if (res.code == 0) {
                    toastr.success(res.msg);
                } else {
                    toastr.error(res.msg);
                }
                setTimeout(function () {
                    $('#register-modal').modal('hide');
                    // 再延迟进行remove
                    setTimeout(function () {
                        $('#register_').removeAttr("disabled");
                    }, 1000);
                }, 1000);
            },
            error: function () {
                toastr.error('请求系统异常');
                setTimeout(function () {
                    $('#register_').removeAttr("disabled");
                }, 1000);
            }
        });

    });
});